#include <openssl/rsa.h>
#include <openssl/err.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "myRsa.h"
/*
int main(void)
{
	char * plainText="Hola@ que tal ?";
	int encSize, resp2;
	char * encrypted;
	char * doubleEncrypted;
	char * semiDecrypted;
	char * plainObtained;
	RSA * rsa, * rsa2,* rsa3,* rsa4 ,* rsaObtained;
	
	rsa=RSA_generate_key(1024,65537,NULL,NULL);
	
	putchar('\n');
	
	rsa2=RSA_generate_key(1024,65537,NULL,NULL);

	encSize = RSA_size(rsa);
	
	//exportPrivateKey(rsa,"privateKey",NULL);
	//importPrivateKey("privateKey",&rsa);
	
	rsa3=RSA_generate_key(1024,65537,NULL,NULL);
	rsa4=RSA_generate_key(1024,65537,NULL,NULL);
	
	
	if ((encrypted = publicEncrypt(plainText, rsa) ) == NULL)
		printf("error");
	
	if ((doubleEncrypted = privateEncrypt(encrypted, rsa2) ) == NULL)
		 printf("error");

	if ((semiDecrypted = publicDecrypt(doubleEncrypted, rsa) ) == NULL)
			 printf("error");
	
	if ((plainObtained = privateDecrypt(semiDecrypted, rsa2) ) == NULL)
			 printf("error\n");
	

	printf("Era: %s con: %d\n",plainText, strlen(plainText));
	printf("Obtuve: %s con %d\n", plainObtained, strlen(plainObtained));
	
}*/
void callback(stage,count,arg)
int stage,count;
char *arg;
{
        FILE *out;

        out=(FILE *)arg;
        fprintf(out,"%d",stage);
        if (stage == 3)
                fprintf(out,"\n");
        fflush(out);
}

int
main(void)
{
        RSA *rsa,*pub_rsa,*priv_rsa;
        int len;
        unsigned char buf[1024],*p;
        

        rsa=RSA_generate_key(512,RSA_F4,NULL,NULL);

        p=buf;

        /* Save the public key into buffer, we know it will be big enough
         * but we should really check how much space we need by calling the
         * i2d functions with a NULL second parameter */
        len=i2d_RSAPublicKey(rsa,&p);
        len+=i2d_RSAPrivateKey(rsa,&p);
        
        man 
        
        printf("BUF: %s\n", buf);

        printf("The public and private key are now both in a char array\n");
        printf("and are taking up %d bytes\n",len);

        RSA_free(rsa);
        p=buf;
        pub_rsa=d2i_RSAPublicKey(NULL,&p,(long)len);
        len-=(p-buf);
        priv_rsa=d2i_RSAPrivateKey(NULL,&p,(long)len);

        if ((pub_rsa == NULL) || (priv_rsa == NULL))
                ERR_print_errors_fp(stderr);
        
        RSA_key2str(pub_rsa,buf);
        printf("\nPUBLICA: %s\n",buf);
        RSA_key2str(priv_rsa,buf);
        printf("\nPRIVADA: %s\n",buf);
        
        RSA_free(pub_rsa);
        RSA_free(priv_rsa);
        }



